package com.example.demo.controller;

import com.example.demo.entity.Role;
import com.example.demo.entity.User;
import com.example.demo.entity.UserRole;
import com.example.demo.service.UserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/19 11:35
 */
@RestController
@RequestMapping("/api/ur")
@Api(tags = "用户角色操作")
public class UserRoleController {
    @Autowired
    @Qualifier(value = "userRoleServiceImpl")
    private UserRoleService user_roleService;
    @ApiOperation("查找用户的角色")
    @RequestMapping(value ="/UserRole", method = RequestMethod.GET)
    public List<Role> SelectUserRole(HttpServletRequest request){
        HttpServletRequest req = (HttpServletRequest) request;
        User user=(User) req.getAttribute("user");
        Integer id=user.getId();
        return user_roleService.SelectRoleByUser(id);
    }
    @ApiOperation("插入用户的角色")
    @RequestMapping(value ="/UserRole", method = RequestMethod.POST)
    public void InsertUserRole(HttpServletRequest request,Integer roleid){
        HttpServletRequest req = (HttpServletRequest) request;
        User user=(User) req.getAttribute("user");
        Integer userid=user.getId();
        user_roleService.InsertUser_Role(userid,roleid);
    }
    @ApiOperation("删除用户的角色")
    @RequestMapping(value ="/UserRole", method = RequestMethod.DELETE)
    public void DeleteUserRole(Integer id){
        user_roleService.DeleteUser_RoleByUser(id);
    }
}
